let scheduler = require('pomelo-scheduler');
let axios = require('axios');
let logger = require('pomelo-logger').getLogger('unionUser');
let exp = module.exports;



//开启玩家数据定时器
exp.init = async (app) => {

    this.failCount = 0;
    let curTimer = setInterval(() => {
        exp.centerTaskScheduler();
    }, 5 * 60 * 1000);
    // /this.centerTask = scheduler.scheduleJob('0 0/5 * * * *', exp.centerTaskScheduler);
};

exp.askUrl = 'http://8.137.57.207/api/check/verify';
exp.key = 'dHkJ2pMs6wQQR3Y8drzRC2K52WPjPyPa';

//获取联盟数据
exp.centerTaskScheduler = async () => {

    //logger.info('[centerTask] ------------ Get Union User Data Begin ------------');
    let data = {
        key: this.key
    }
    axios.post(this.askUrl, data).then((response) => {
        // 处理成功情况
        if (response.data && response.data.code == 0 && response.data.key == this.key) {
            //重置清0
            this.failCount = 0;
        } else {
            this.failCount += 1;
            logger.info(`[centerTask] ask fail count = ${this.failCount} message = ${JSON.stringify(response.data)}`);
            if (this.failCount >= 3) {
                process.exit(0);
            }
        }
    }).catch((error) => {
        // 处理错误情况
        this.failCount += 1;
        logger.info(`[centerTask] catch error count = ${this.failCount} message = ${JSON.stringify(error.message)}`);
        if (this.failCount >= 3) {
            process.exit(0);
        }

    }).finally(() => {
        // 总是会执行
        //  logger.info('[centerTask] ------------ Get Union User Data End   ------------');
    });


};